package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddCateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {
    @Autowired
    private IYddCateService iYddCateService;


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R getById(Long id) {
        //执行
        YddCate yddCate = iYddCateService.getById(id);
        //返回
        R r = R.success("id", id);
        r.putData("catename", yddCate.getCatename());
        r.putData("sort_num", yddCate.getSortNum());
        r.putData("icon", yddCate.getIcon());
        r.putData("default_data", yddCate.getDefaultData());

        return r;
    }


    /**
     * 分类列表--分页
     *
     * @param page
     * @param limit
     * @param catename
     * @return
     */
    @GetMapping("/list")
    public R page(int page, int limit, String catename) {
        //分页查询器
        Page<YddCate> yddPage = new Page<>(page, limit);

        //条件构造器
        LambdaQueryWrapper<YddCate> yddCateLQW = new LambdaQueryWrapper<>();
        //添加条件--cateName 模糊查询
        yddCateLQW.like(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        //添加条件--cateName 排序数字
        yddCateLQW.orderByAsc(YddCate::getSortNum);

        //执行
        iYddCateService.page(yddPage, yddCateLQW);

        //返回
        R r = R.success("totalPages", (int)yddPage.getPages());
        r.putData("pageSize", yddPage.getSize());
        r.putData("currentPage", yddPage.getCurrent());
        r.putData("count", (int)yddPage.getTotal());
        r.putData("data", yddPage.getRecords());

        return r;

    }


    /**
     * 添加数据
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody YddCate yddCate) {

        yddCate.setDefaultData("1");
        iYddCateService.save(yddCate);

        R r = new R();
        r.setErrno(0);
        return r;

    }


    /**
     * 分类删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delete(Long id) {


        iYddCateService.removeById(id);

        //返回
        R r = new R();
        r.setErrno(0);
        return r;

    }


    /**
     * 修改分类
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddCate yddCate) {

        iYddCateService.updateById(yddCate);

        //返回
        R r = new R();
        r.setErrno(0);
        return r;
    }

    /**
     * 分类查询--全部
     *
     * @return
     */

    @GetMapping("/all")
    public ResultData getAll() {

        List<YddCate> yddCates = iYddCateService.list();
        return ResultData.ok("",yddCates);

    }
}
